<?php
// Entity/Panier.php
namespace sil29\VitrineBundle\Entity;

class Panier {
	private $contenu;
	public function __construct() {
		$this->contenu = array ();
	}
	public function getContenu() {
		return $this->contenu;
	}
	public function ajoutArticle($article) {
		if (array_key_exists ( $article->getId(), $this->contenu )) {
				$this->contenu [$article->getId()][1] = $this->contenu [$article->getId()][1] + 1;			
		} else {
			$this->contenu [$article->getId()] = array($article,1);
		}
	}
	
	public function diminuerArticle($article) {
		if (array_key_exists ( $article->getId(), $this->contenu )) {
				
			$this->contenu [$article->getId()][1] = $this->contenu [$article->getId()][1] - 1;
		} else {
			$this->contenu [$article->getId()] = array($article,1);
		}
	}
	
	
	public function supprimeArticle($article) {
		if (array_key_exists ( $article->getId(), $this->contenu )) {
				unset ( $this->contenu [$article->getId()] );
		}
	}
	public function viderPanier() {
		unset ( $this->contenu );
		$this->contenu = array ();
	}
	public function getMontantPanier() {
		$montant = 0;
		foreach ( $this->contenu as $ligneArticle ) {
			$montant = $montant + ($ligneArticle[0]->getPrix () * $ligneArticle[1]);
		}
		return $montant;
	}
	public function getNombreArticles() {
		$nombreArticles = 0;
		foreach ( $this->contenu as $ligneArticle) {
			$nombreArticles = $nombreArticles + $ligneArticle[1];
		}
		return $nombreArticles;
	}
}
?>